package nieflash.display {
	import com.greensock.TweenLite;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class switcher extends EventDispatcher {
		public var currentID:int = -1;
		public var itemList:Array = [];
		public var pageSizes:Array = [];
		public var Root:DisplayObjectContainer;
		
		private var currentLoader:Loader;
		private var loadingLoader:Loader;
		private var randomID:int;
		
		private var _progressCallBack:Function;
		private var _completeCallBack:Function;
		private var _changeCallBack:Function;
		private var _hideItemFunction:Function;
		private var _showItemFunction:Function;
		
		public static const SWITCHER_ERROR:String = "switcher_error";
		
		public function switcher(_root:DisplayObjectContainer, _itemList:Array, _startID:int = 0, _pageSizes:Array = null):void {
			itemList = _itemList;
			Root = _root;
			if (_pageSizes != null) {
				pageSizes = _pageSizes;
			}
			hideItemFunction = hideNormal;
			showItemFunction = showNormal;
		}
		
		public function changePage(_id:int):void {
			if (_changeCallBack != null) {
				_changeCallBack();
			}
			loadItem(_id);
		}
		
		public function set changeCallBack(_callback:Function):void {
			_changeCallBack = _callback;
		}
		
		public function set progressCallBack(_callback:Function):void {
			_progressCallBack = _callback;
		}
		
		public function set completeCallBack(_callback:Function):void {
			_completeCallBack = _callback;
		}
		
		public function set hideItemFunction(_function:Function):void {
			_hideItemFunction = _function;
		}
		
		public function set showItemFunction(_function:Function):void {
			_showItemFunction = _function;
		}
		
		public function unloadCurrentItem():void {
			if (currentLoader != null) {
				_hideItemFunction(currentLoader);
				currentLoader = null;
			}
			if (loadingLoader != null) {
				try {
					loadingLoader.close();
					loadingLoader.unloadAndStop();
					loadingLoader = null;
					trace("switcher:卸载当前加载");
				}catch (e:Error) {
					trace("switcher:当前加载已完成,close无效");
				}
			}
			currentID = -1;
		}
		
		private function loadItem(_id:int):void {
			if (currentID == _id) {
				trace("switcher:不能加载当前页");
				this.dispatchEvent(new Event(SWITCHER_ERROR));
				return;
			}
			if (currentLoader != null) {
				_hideItemFunction(currentLoader);
				currentLoader = null;
			}
			if (loadingLoader != null) {
				try {
					loadingLoader.close();
					loadingLoader.unloadAndStop();
					loadingLoader = null;
					trace("switcher:卸载当前加载");
				}catch (e:Error) {
					trace("switcher:当前加载已完成,close无效");
				}
			}
			currentID = _id;
			var _randomID:int = Math.random() * 10000;
			randomID = _randomID;
			var _loader:Loader = new Loader();
			if (_progressCallBack != null) {
				_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, function(evt:ProgressEvent) {
					if (currentID == _id && randomID == _randomID) { 
						var _num:int = evt.bytesLoaded / (evt.bytesTotal > 0?evt.bytesTotal:pageSizes[_id]) * 100;
						_progressCallBack(_num);
					}
				});
			}
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function() {
				loadingLoader = null;
				if (currentID == _id && randomID == _randomID) {
					if (_completeCallBack != null) {
						_completeCallBack();
					}
					currentLoader = _loader;
					_showItemFunction(_loader);
				}else {
					_loader.unloadAndStop();
				}
				trace("switcher| 加载: " + itemList[_id] + " |大小: " + _loader.contentLoaderInfo.bytesTotal);
			});
			_loader.load(new URLRequest(itemList[_id]));
			loadingLoader = _loader;
		}
		
		private function hideNormal(_tar:Loader):void {
			TweenLite.to(_tar, 0.5, { alpha:0, onComplete:function() {
				Root.removeChild(_tar);
				_tar.unloadAndStop();
				_tar = null;
			}});
		}
		
		private function showNormal(_tar:Loader):void {
			_tar.alpha = 0;
			Root.addChild(_tar);
			TweenLite.to(_tar, 0.5, { alpha:1 } );
		}
	}
}